//
//  singleResume.swift
//  万才信息App
//
//  Created by 鹏速科技 on 16/8/1.
//  Copyright © 2016年 万才信息. All rights reserved.
//

import UIKit
import Material

class singleResumeController:UITableViewController{
    
    var resumeId:String = ""
    var resume = detailResume()
    

////    @IBOutlet weak var tvBasicInfo: UILabel!
//    var tvBasicInfo = UILabel()
////    @IBOutlet weak var tvObjWork: UILabel!
//    var tvObjWork = UILabel()
//    var tvResumeInfo = UILabel()
    
//    简单数据
    var basicInfo:String = "1"
    var objWork:String = "1"
    var resumeInfo:String = "1"
    
//    var lvLanguage = UITableView(frame: CGRectMake(15, 0, 600, 0))
    
    var languageSource = languageDataSource()
    var workExpSource = workExpDataSource()
    var trainSource = trainExpDataSource()
    var eduSource = eduDataSource()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        setupView()
        loadDetailResume()
    }
    
    override func viewDidAppear(animated: Bool) {
        loadDetailResume()
    }
    
//    TODO:加载View部分：-------------------------------------------------------------------------------------------------------------
//    表格的对象
    
    @IBOutlet var singleViewTable: UITableView!
    
    private func setupView(){
//     默认的高度
        self.tableView.estimatedRowHeight = 44
        self.tableView.rowHeight = UITableViewAutomaticDimension
        self.title = resume.ResumeName
        singleViewTable.contentInset = UIEdgeInsetsMake(22, 0, 0, 0);
//        singleViewTable.layer.borderWidth = 10
//        singleViewTable.backgroundColor = UIColor.hexStringToColor("#EFEFEF")
//        singleViewTable.layer.borderColor = UIColor.hexStringToColor("#EFEFEF").CGColor
    }
    //        自适应高度刷新
    private func autoRefresh(){
        self.tableView.rowHeight = UITableViewAutomaticDimension
        self.tableView.reloadData()
    }
    
//    有几个部分？
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 7
    }

//    每个分组上边的预留空白高度
    override func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if(section == 6){
            return 0.0
        }
        return 44.0
    }

//    每个分组下边的留空高度
    override func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }
    
//    section长什么样
    override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        switch section {
        case 0:
            let  headerCell:basicHeaderCell = self.tableView.dequeueReusableCellWithIdentifier("basicHeaderCell") as! basicHeaderCell
            headerCell.setTitle("基本信息")
            return headerCell
        case 1:
            let  headerCell:basicHeaderCell = self.tableView.dequeueReusableCellWithIdentifier("basicHeaderCell") as! basicHeaderCell
            headerCell.setTitle("工作意向")
            return headerCell
        case 2:
            let  headerCell:addHeaderCell = self.tableView.dequeueReusableCellWithIdentifier("addHeaderCell") as! addHeaderCell
            headerCell.setTitle("外语能力")
            if(languageSource.languageList.count == 0){
                headerCell.setTitle("点击右边的按钮添加外语能力：")
                headerCell.tvTitle.font = UIFont.systemFontOfSize(17.0)
            }
            headerCell.setSegue("toAddLan", controller: self)
            return headerCell
        case 3:
            let  headerCell:addHeaderCell = self.tableView.dequeueReusableCellWithIdentifier("addHeaderCell") as! addHeaderCell
            headerCell.setTitle("工作经历")
            if(workExpSource.workExpList.count == 0){
                headerCell.setTitle("点击右边的按钮添加工作经历：")
                headerCell.tvTitle.font = UIFont.systemFontOfSize(17.0)
            }
            headerCell.setSegue("toAddWork", controller: self)
            return headerCell
        case 4:
            let  headerCell:addHeaderCell = self.tableView.dequeueReusableCellWithIdentifier("addHeaderCell") as! addHeaderCell
            headerCell.setTitle("培训经历")
            if(trainSource.trainExpList.count == 0){
                headerCell.setTitle("点击右边的按钮添加培训经历：")
                headerCell.tvTitle.font = UIFont.systemFontOfSize(17.0)
            }
            headerCell.setSegue("toAddTrain", controller: self)
            return headerCell
        case 5:
            let  headerCell:addHeaderCell = self.tableView.dequeueReusableCellWithIdentifier("addHeaderCell") as! addHeaderCell
            headerCell.setTitle("教育经历")
            if(eduSource.eduList.count == 0){
                headerCell.setTitle("点击右边的按钮添加教育经历：")
                headerCell.tvTitle.font = UIFont.systemFontOfSize(17.0)
            }
            headerCell.setSegue("toAddEdu", controller: self)
            return headerCell

        default:
            break;
        }
        return UIView()
    }
    
//    header高度
    
//    footer高度设置为0
    
    //    每个section下面的行数
        override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            switch section {
                case 0:
//                    基本信息
                    return 1
                case 1:
//                    工作意向
                    return 1
                case 2:
//                    外语能力
                    return languageSource.languageList.count
                case 3:
//                    工作经历
                    return workExpSource.workExpList.count
                case 4:
//                    培训经历
                    return trainSource.trainExpList.count
                case 5:
//                    教育经历
                    return eduSource.eduList.count
                case 6:
//                    是否公开
                    return 1
                default:
                    break;
            }
            return 0
        }
    
    //    设置每个行的内容：主要
        override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            switch indexPath.section {
//                case 0:
//                    let cellIndentifer="iconCell"
//                    let cell:UITableViewCell=tableView.dequeueReusableCellWithIdentifier(cellIndentifer)! as UITableViewCell
//                    //                    设置外观：
//                    let tmp:UIView = UIView()
//                    cell.backgroundView = tmp
//                    tmp.layer.borderWidth = 5
//                    //        tmp.layer.cornerRadius = 5
//                    tmp.layer.borderColor = UIColor.hexStringToColor("#EFEFEF").CGColor
//                    //                    返回cell
//                    return cell

                case 0:
//                    基本信息
                    let cellIndentifer="autoHeightLabelCell"
                    let cell:autoHeightLabelCell!=tableView.dequeueReusableCellWithIdentifier(cellIndentifer) as! autoHeightLabelCell
                    cell.setInfo(basicInfo)
                    cell.controller = self
                    cell.segueInden = "toUpdBasicInfo"
//                    设置外观：
                    let tmp:UIView = UIView()
                    cell.backgroundView = tmp
                    tmp.layer.borderWidth = 5
                    //        tmp.layer.cornerRadius = 5
                    tmp.layer.borderColor = UIColor.hexStringToColor("#EFEFEF").CGColor
//                    返回cell
                    return cell
                case 1:
//                    意向工作
                    let cellIndentifer="autoHeightLabelCell"
                    let cell:autoHeightLabelCell!=tableView.dequeueReusableCellWithIdentifier(cellIndentifer) as! autoHeightLabelCell
                    cell.setInfo(objWork)
                    cell.controller = self
                    cell.segueInden = "toUpdObjWork"
                    //                    设置外观：
                    let tmp:UIView = UIView()
                    cell.backgroundView = tmp
                    tmp.layer.borderWidth = 5
                    //        tmp.layer.cornerRadius = 5
                    tmp.layer.borderColor = UIColor.hexStringToColor("#EFEFEF").CGColor
                    //                    返回cell
                    return cell
                case 2:
//                    外语能力
                    let cellIndentifer="languageCell"
                    let cell:languageCell!=tableView.dequeueReusableCellWithIdentifier(cellIndentifer) as! languageCell
                    cell.controller = self
                    cell.setLanguage(languageSource.languageList[indexPath.row])
                    //                    设置外观：
                    let tmp:UIView = UIView()
                    cell.backgroundView = tmp
                    tmp.layer.borderWidth = 5
                    //        tmp.layer.cornerRadius = 5
                    tmp.layer.borderColor = UIColor.hexStringToColor("#EFEFEF").CGColor
                    //                    返回cell
                    return cell
                case 3:
//                    工作经历
                    let cellIndentifer="workCell"
                    let cell:workCell!=tableView.dequeueReusableCellWithIdentifier(cellIndentifer) as! workCell
                    cell.controller = self
                    cell.setWork(workExpSource.workExpList[indexPath.row])
                    //                    设置外观：
                    let tmp:UIView = UIView()
                    cell.backgroundView = tmp
                    tmp.layer.borderWidth = 5
                    //        tmp.layer.cornerRadius = 5
                    tmp.layer.borderColor = UIColor.hexStringToColor("#EFEFEF").CGColor
                    //                    返回cell
                    return cell
                case 4:
//                    培训经历
                    let cellIndentifer="trainCell"
                    let cell:trainCell!=tableView.dequeueReusableCellWithIdentifier(cellIndentifer) as! trainCell
                    cell.controller = self
                    cell.setTrain(trainSource.trainExpList[indexPath.row])
                    //                    设置外观：
                    let tmp:UIView = UIView()
                    cell.backgroundView = tmp
                    tmp.layer.borderWidth = 5
                    //        tmp.layer.cornerRadius = 5
                    tmp.layer.borderColor = UIColor.hexStringToColor("#EFEFEF").CGColor
                    //                    返回cell
                    return cell
                case 5:
//                    教育经历
                    let cellIndentifer="eduCell"
                    let cell:eduCell!=tableView.dequeueReusableCellWithIdentifier(cellIndentifer) as! eduCell
                    cell.controller = self
                    cell.setEdu(eduSource.eduList[indexPath.row])
                    //                    设置外观：
                    let tmp:UIView = UIView()
                    cell.backgroundView = tmp
                    tmp.layer.borderWidth = 5
                    //        tmp.layer.cornerRadius = 5
                    tmp.layer.borderColor = UIColor.hexStringToColor("#EFEFEF").CGColor
                    //                    返回cell
                    return cell
                case 6:
//                    是否公开
                    let cellIndentifer="isPrivacyCell"
                    let cell:isPrivacyCell!=tableView.dequeueReusableCellWithIdentifier(cellIndentifer) as! isPrivacyCell
                    cell.controller = self
                    let privacy = resume.Privacy
                    var isOpen:Bool = false
                    if privacy == "1" {
                        isOpen = true
                    }
                    cell.setOpen(isOpen, resumeId: resume.ResumeId, controller: self)
//                    设置外观：
                    let tmp:UIView = UIView()
                    cell.backgroundView = tmp
                    tmp.layer.borderWidth = 5
                    //        tmp.layer.cornerRadius = 5
                    tmp.layer.borderColor = UIColor.hexStringToColor("#EFEFEF").CGColor
                    //                    返回cell
                    return cell

                default:
                    break;
            }
            
            let cell = UITableViewCell()
            return cell
        }


    
    
    

    
//------------------------------------------------------------------------------------------------------------------------------------
    
//    加载简历详细信息
    private func loadDetailResume(){
        let waiting = waitingAlertUtil(msg: "加载中")
        waiting.show(self)
        resumeManager.GetResumeBasicInfoById(self.resumeId, successCallback: { (theRetList) in
            waiting.stop()
            if(theRetList.count == 0){
                self.navigationController?.popViewControllerAnimated(true)
                return
            }
            self.resume = theRetList[0]
//            TODO:加载简历信息 共4个
            var resumeInfo :String = "简历名称:    "+self.resume.ResumeName+"\n"
            resumeInfo += "创建时间:    "+self.resume.CreateTime+"\n"
            resumeInfo += "更新时间:    "+self.resume.Up2+"\n"
            let privacy = self.resume.Privacy=="1" ? "公开" : "私密"
            resumeInfo += "公开程度:    "+privacy
            print(resumeInfo)
            self.resumeInfo = resumeInfo
//            TODO:添加基本信息 共10个
            var basicInfo:String = "姓        名："+self.resume.UserName+"\n"
            basicInfo += "性        别："+self.resume.Gender+"\n"
            basicInfo += "婚姻状况："+self.resume.Marriage+"\n"
            let DateFormatter=NSDateFormatter()
            DateFormatter.dateFormat="yyyy/M/d H:mm:ss"
            let convertDate=DateFormatter.dateFromString(self.resume.Birthday)
            DateFormatter.dateFormat="yyyy/M/d"
            let trueBirthday = DateFormatter.stringFromDate(convertDate!)
            basicInfo += "生        日："+trueBirthday+"\n"
            basicInfo += "最高学历："+self.resume.HighDegree+"\n"
            basicInfo += "工作年限："+self.resume.WorkYears+"\n"
            basicInfo += "现  居  地："+self.resume.xjdLocaltion+"\n"
            basicInfo += "手        机："+self.resume.Mobile+"\n"
            basicInfo += "邮        箱："+self.resume.Email
            print(basicInfo)
            self.basicInfo = basicInfo
//            TODO:添加工作意向 共5个
            var jobObj:String = "期望行业："+self.resume.IndChineseName+"\n"
            jobObj += "期望职位："+self.resume.FunName+"\n"
            jobObj += "工作性质："+self.resume.JobNature+"\n"
            jobObj += "期望薪资："+self.resume.ExpectSalary+"\n"
            jobObj += "期望工作地点："+self.resume.CityName
            print(jobObj)
            self.objWork = jobObj
            self.autoRefresh()
//            添加四项经历
            self.title = self.resume.ResumeName
            self.loadLanguage()
            self.loadEdu()
            self.loadWork()
            self.loadTrain()
            }) { (error) in
                waiting.stop()
                warningAlertUtil(title: "错误", msg: error).show(self)
        }
    }
    
//    TODO:加载外语能力
    private func loadLanguage(){
        resumeManager.GetLanguageAbilityList(resumeId, successCallback: { (theRetList) in
            self.languageSource.languageList = theRetList
//            显示操作：
            print("成功获得外语能力")
            self.autoRefresh()
            }) { (error) in
                warningAlertUtil(title: "错误", msg: error).show(self)
        }
    }
    
//    TODO:添加工作经验
    private func loadWork(){
        resumeManager.GetWorkExperienceList(resumeId, successCallback: { (theRetList) in
            self.workExpSource.workExpList = theRetList
//            显示操作：
            print("成功获得工作经验")
            self.autoRefresh()
        }) { (error) in
            warningAlertUtil(title: "错误", msg: error).show(self)
        }
    }
    
//    TODO:添加培训经验
    private func loadTrain(){
        resumeManager.GetTrainExperienceList(resumeId, successCallback: { (theRetList) in
            self.trainSource.trainExpList = theRetList
//            显示操作
            print("成功获得培训经验")
            self.autoRefresh()
            }) { (error) in
                warningAlertUtil(title: "错误", msg: error).show(self)
        }
    }
    
//    TODO:添加教育经历
    private func loadEdu(){
        resumeManager.GetEducationList(resumeId, successCallback: { (theRetList) in
            self.eduSource.eduList = theRetList
//            显示操作
            print("成功获得教育经历")
            self.autoRefresh()
        }) { (error) in
            warningAlertUtil(title: "错误", msg: error).show(self)
        }

    }
    
    
//    segue传值处！
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        print("segueIden=")
        print(segue.identifier)
        print("segue传值")
        print("传输了一个resumeId和sender:")
        print(sender)
        print(resumeId)
        if segue.identifier == "toAddLan" {
//            添加外语
            print("segue:toAddLan")
            (segue.destinationViewController as! addLanguageController).resumeId = resume.ResumeId
        }else if segue.identifier == "toAddEdu" {
//            添加教育
            print("toAddEducation")
            (segue.destinationViewController as! addEducationController).resumeId = resume.ResumeId
        }else if segue.identifier == "toAddWork" {
//           添加工作
            print("toAddWork")
            (segue.destinationViewController as! addWorkExperienceController).resumeId = resume.ResumeId
        }else if segue.identifier == "toAddTrain" {
//            添加培训
            print("toAddTrain")
            (segue.destinationViewController as! addTrainController).resumeId = resume.ResumeId
        }else if segue.identifier == "toUpdLan" {
//            更新外语能力
            print("toUpdLan")
            print(sender)
            let dest:updateLanguageController=segue.destinationViewController as! updateLanguageController
            dest.language=sender as! languageAbility
            dest.resumeId=resumeId
        }else if segue.identifier == "toUpdWork" {
//            更新工作经历
            print("toUpdWork")
            print(sender)
            let dest:updateWorkExperienceController=segue.destinationViewController as! updateWorkExperienceController
            dest.work = sender as! workExperience
            dest.resumeId=resumeId
        }else if segue.identifier == "toUpdTrain" {
//            更新培训
            print("toUpdTrain")
            print(sender)
            let dest:updateTrainExperience=segue.destinationViewController as! updateTrainExperience
            dest.train = sender as! trainExperience
            dest.resumeId=resumeId
        }else if segue.identifier == "toUpdEdu" {
//            更新教育
            print("toUpdEdu")
            print(sender)
            let dest:updateEducationController=segue.destinationViewController as! updateEducationController
            dest.education = sender as! listEduExperience
            dest.resumeId=resumeId
        }else if segue.identifier == "toUpdBasicInfo" {
//            更新基本信息
            print("toUpdBasicInfo")
            let dest:updateResumeBasicInfoController=segue.destinationViewController as! updateResumeBasicInfoController
            dest.resume = self.resume
        }else if segue.identifier == "toUpdObjWork" {
//            更新工作意向
            print("toUpdObjWork")
            let dest:updateResumeObjController=segue.destinationViewController as! updateResumeObjController
            dest.resume = self.resume
            
        }


    }

    
}


